﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import Goodshearder from './goodshearder'
import CommonJs from '../../utils/common'
import './address.scss'

export default class Index extends Component {
  constructor(props) {  
  	super(props);
    this.state = {
      alertnum:0,
	  mylist:[],
      region:'',
	  zip:'',
	  name:'',
	  mobile:'',
	  address:'',
	  editindex:-1,
	  from:0
    }
  }
  config = {
    navigationBarTitleText: '加载中'
  }

  componentWillMount () {
    Taro.setNavigationBarTitle({title:"我的地址"})
	if(this.$router.preload){
		var type=this.$router.preload.type;
		if(type=='order'){
			this.setState({
			  from:1
			})
		}
	}
    this.getaddress()
  }
  componentDidShow () {
    this.setState({
      region: Taro.getStorageSync('region')
    })
  }
  getInputVal(event){
  	var m=event.target;
	var obj={};
	obj[m.id]=m.value
    this.setState(obj)
  }
  open(url,type){
    this.$preload({
      type:type
    })
    CommonJs.openTo(url);
  }
  getaddress(){
    //获取地址列表
	var _this=this;
	var dateinfo = Taro.getStorageSync('dateinfo');
    var url='/shop/papi/v1.9/address?status.equals=SO&accountId.equals='+dateinfo.userid;
	//var url='/shop/papi/v1.9/address/find/account'
    CommonJs.ajaxRequest(url,'','GET',function(res){
      _this.setState({
		mylist: res.data.list
	  })
    })
    
  }
  opennewaddress(){
		this.setState({
			alertnum:1,
			editindex:-1,
			zip:'',
		  name:'',
		  mobile:'',
		  address:''
		})
  }
  closeaddress(){
	this.setState({
		alertnum:0
	}) 
  }
  edit(index){
	var mylist=this.state.mylist;
	this.setState({
		alertnum:1,
		region:mylist[index].region,
		zip:mylist[index].zip,
		name:mylist[index].name,
		mobile:mylist[index].mobile,
		address:mylist[index].address,
		editindex:index
	})  
  }
  del(index){
	var mylist=this.state.mylist;
	var url='/shop/papi/v1.9/address/updateStatus?id='+mylist[index].id
	var _this=this;
	Taro.showModal({
	  title: '操作提示',
	  content: '是否确定删除',
	  success(ret){
		  if(ret.confirm){
			CommonJs.ajaxRequest(url,'','PUT',function(res){
			  Taro.showToast({
					title:'操作成功',
					icon: 'none',
					duration: 1500
				})
			  _this.getaddress();
			})
		  }
		}
	})
  }
  choose(index){
	var mylist=this.state.mylist;
	var url='/shop/papi/v1.9/address/firstChoice?id='+mylist[index].id
	var _this=this;
	Taro.showModal({
	  title: '操作提示',
	  content: '是否确定',
	  success(ret){
		  if(ret.confirm){
			CommonJs.ajaxRequest(url,'','PUT',function(res){
			  Taro.showToast({
					title:'操作成功',
					icon: 'none',
					duration: 1500
				})
			  _this.getaddress();
			})
		  }
		}
	})
  }
  doit(index){
  	
	if(this.state.from==1){
		var mylist=this.state.mylist;
		let pages = getCurrentPages();
		let prevPage = pages[pages.length-2];
		prevPage.setData({
		  data:mylist[index]
		});
		Taro.navigateBack({
		  delta: 1
		})
	}
  }
  lijipay(){
	var dateinfo = Taro.getStorageSync('dateinfo');
	var state=this.state;
	var dates={
	  "accountId": dateinfo.userid,
	  "address": state.address,
	  "email": "",
	  "isFirst": "NO",
	  "latitude": 0,
	  "locationAddress": "",
	  "longitude": 0,
	  "mobile":state.mobile,
	  "name": state.name,
	  "phone": "",
	  "region":state.region,
	  "status": "SO",
	  "type": "DESTINATION",
	  "zip": state.zip
	}
	var method='POST';
	
	if(state.editindex>=0){
		var mylist=this.state.mylist;
		var index=state.editindex;
		dates.id=mylist[index].id;
		dates.isFirst=mylist[index].isFirst;
		method='PUT';
	}
	let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/  
	if(!myreg.test(dates.mobile)){
		Taro.showToast({
			title:'请输入正确的手机号码',
			icon: 'none',
			duration: 1500
		})
	}
	else if(!dates.address){
		Taro.showToast({
			title:'详细地址不能为空',
			icon: 'none',
			duration: 1500
		})
	}
	else if(!dates.region){
		Taro.showToast({
			title:'所在地区不能为空',
			icon: 'none',
			duration: 1500
		})
	}
	else if(!dates.name){
		Taro.showToast({
			title:'收货人不能为空',
			icon: 'none',
			duration: 1500
		})
	}
	else{
		var url='/shop/papi/v1.9/address'
		var _this=this;
		//console.log(JSON.stringify(dates)+"//eee"+method)
		CommonJs.ajaxRequest(url,dates,method,function(res){
		  _this.closeaddress()
		  _this.getaddress();
		})
	}
    
  }
  
  render () {
    const mylist = this.state.mylist
    let editcontent = this.state.editcontent
    const from=this.state.from;
    return (
      <View className='page'>
        <View className='address-news' style={mylist.length>0?'display:block':'display:none'}>
          {mylist.map((item, index) => (
            <View className='address' onClick={this.doit.bind(this,index)}>
              <View className='address-name'>
				<Text className='address-mz'>{item.name}</Text>
				<Text className='address-sj'>{item.mobile}</Text>
			  </View>
              <View className='address-xx'>{item.regionName}{item.address}</View>
			  <View className='address-other' style={from==0?'display:block':'display:none'}>
				<View className='address-edit' onClick={this.del.bind(this,index)}>删除</View>
				<View className='address-edit' onClick={this.edit.bind(this,index)}>编辑</View>
				<View className='address-isfrist' onClick={this.choose.bind(this,index)}>{item.isFirst=='YES'?'默认地址':'设置为默认'}</View>
			  </View>
            </View>
          ))}
        </View>
		<View className='address-news' style={mylist.length>0?'display:none':'display:block'}>
			<View className='qun-znwn'>暂无地址</View>
		</View>
        <View className='add-address' onClick={this.opennewaddress.bind(this)}>新增收货地址</View>
        <View className='bg-newaddress' style={this.state.alertnum == 1?'display:block':'display:none'}>
          <View className='news-address'>
            <View className='address-close' onClick={this.closeaddress.bind(this)}>X</View>
            <View className='address-mes'>收货人信息</View>
            <View className='address-content'>
              <View className='address-contact'>
                <View className='address-title'>
                  <Text className='address-star'>*</Text><Text className='address-people'>收货人:</Text>
                </View>
                <View className='address-into'>
                  <View className='address-intocon'>
                    <Input className='' value={this.state.name} id='name' onInput={this.getInputVal.bind(this)} type='text' placeholder='请输入收货人姓名'/>
                  </View>
                </View>
              </View>
              <View className='address-contact'>
                <View className='address-title'>
                  <Text className='address-star'>*</Text><Text className='address-people'>手机号码:</Text>
                </View>
                <View className='address-into'>
                  <View className='address-intocon'>
                    <Input className='' value={this.state.mobile} maxLength='11' id='mobile' onInput={this.getInputVal.bind(this)} type='number' placeholder='请输入您的手机号码'/>
                  </View>
                </View>
              </View>
              <View className='address-contact' onClick={this.open.bind(this,'/pages/my/region')}>
                <View className='address-title'>
                  <Text className='address-star'>*</Text><Text className='address-people'>所在地区:</Text>
                </View>
                <View className='address-into'>
                  <View className='address-intocon'>
                    <Input className='' disabled='true' value={this.state.region.name} type='text' placeholder='请选择所在地区'/>
                  </View>
                </View>
              </View>
              <View className='address-contact'>
                <View className='address-title'>
                  <Text className='address-star'>*</Text><Text className='address-people'>详细地址:</Text>
                </View>
                <View className='address-into'>
                  <View className='address-intocon'>
                    <Input className='' value={this.state.address}  id='address' onInput={this.getInputVal.bind(this)} type='text' placeholder='请输入详细地址'/>
                  </View>
                </View>
              </View>
			  			<View className='address-contact'>
                <View className='address-title'>
                  <Text className='address-star'></Text><Text className='address-people'>邮编:</Text>
                </View>
                <View className='address-into'>
                  <View className='address-intocon'>
                    <Input className='' value={this.state.zip} id='zip' onInput={this.getInputVal.bind(this)} type='text' placeholder='请输入邮编'/>
                  </View>
                </View>
              </View>
            </View>
            <View className='push-address' onClick={this.lijipay.bind(this)}>提交</View>
          </View>
        </View>
      </View>
    )
  }
}

 